<template>
  <div id="ycpieparams" :class="className" :style="{height:height,width:width}" >{{ ycPieQueryResult }}</div>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './../mixins/resize'
  import {listYcPieByParams} from "../../../api/dashboard/ycpie";

  const animationDuration = 3000

  export default {
    mixins: [resize],
    props: {
      ycPieQueryResult:{
        type: Object,
      },
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    data() {
      return {
        dataResult:{
          indeId:null,
          dataYear:null,
        },
        MapList:[],
        MapListPie:[],
        rows:[],
        name:[],
        count:[],
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },

    watch: {
      ycPieQueryResult: {
        handler(newValue) {
          // console.log("watch: 监听到的newValue数据如下"+newValue)
          // console.log(newValue)
          this.emptyChart();
          this.getPieList(newValue);
          // listYcBar(newValue);
        },
        deep: true
      }
    },

    methods: {
      async getPieList(newValue){
        this.dataResult.indeId = newValue.indeId;
        this.dataResult.dataYear = newValue.dataYear;
        await listYcPieByParams(this.dataResult).then(response => {
          this.MapListPie = response.rows;
          // console.log(this.MapListPie)
          });
        this.chart.setOption({
          title: {
            text: '个体指标饼状图',
            subtext: '本图统计单个指标在某年的达标情况',
            left: 'center'
          },
          tooltip: {
            trigger: 'item',
            formatter: "{b} : {d}%",
          },
          legend: {
            bottom: 10,
            left: 'center',
            data: ['达标率', '未达标率']
          },
          toolbox: {
            show: true,
            feature: {
              mark: { show: true },
              dataView: { show: true, readOnly: false },
              restore: { show: true },
              saveAsImage: { show: true }
            }
          },
          series: [
            {
              type: 'pie',
              radius: '60%',
              center: ['50%', '50%'],
              selectedMode: 'single',
              itemStyle:{
                normal:{
                  label:{
                    show: true,
                    formatter: '{b} : {d}%'
                  },
                  labelLine :{show:true}
                }
              },
              data: [
                { value: this.MapListPie[0].qulified,name: '达标率' },
                { value: this.MapListPie[0].unqulified, name: '未达标率' }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        })

      },

      async initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          title: {
            text: '个体指标饼状图',
            subtext: '本图统计单个指标在某年的达标情况',
            left: 'center'
          },
          tooltip: {
            trigger: 'item',
          },
          legend: {
            bottom: 10,
            left: 'center',
            data: []
          },
          toolbox: {
            show: true,
            feature: {
              mark: { show: true },
              dataView: { show: true, readOnly: false },
              restore: { show: true },
              saveAsImage: { show: true }
            }
          },
          series: [
            {
              type: 'pie',
              radius: '65%',
              center: ['50%', '50%'],
              selectedMode: 'single',
              data: [
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        })
      },
      emptyChart(){
        this.chart.clear();
        this.chart.setOption(
          {
            tooltip: {
              trigger: 'item',
            },
            legend: {
              bottom: 10,
              left: 'center',
              data: []
            },
            toolbox: {
              show: true,
              feature: {
                mark: { show: true },
                dataView: { show: true, readOnly: false },
                restore: { show: true },
                saveAsImage: { show: true }
              }
            },
            series: [
              {
                type: 'pie',
                radius: '65%',
                center: ['50%', '50%'],
                selectedMode: 'single',
                data: [],
                emphasis: {
                  itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          },true)
      },
    }
  }
</script>
